package com.group2.view;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import model.*;
import static model.netBase.IP;
public class LibraryAddBookFrame extends JFrame {
	private JPanel contentPane;
	private JTextField textFieldBookName;
	private JTextField textFieldAuthor;
	private JTextField textFieldPublisher;
	private JComboBox comboBox = new JComboBox();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LibraryAddBookFrame frame = new LibraryAddBookFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LibraryAddBookFrame() {
		setTitle("图书入库");
		setBounds(500, 180, 372, 328);
		this.setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		this.setResizable(false);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "\u56FE\u4E66\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JButton btnNewButton = new JButton("入库");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					bookAddActionPerformed(e);
				} catch (IOException ex) {
					throw new RuntimeException(ex);
				}
			}
		});
		
		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValueActionPerformed(e);
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap(81, Short.MAX_VALUE)
					.addComponent(btnNewButton)
					.addGap(50)
					.addComponent(btnNewButton_1)
					.addGap(87))
				.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
					.addGap(18)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 307, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(23, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(33)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 186, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton_1)
						.addComponent(btnNewButton))
					.addContainerGap(21, Short.MAX_VALUE))
		);
		
		JLabel lblNewLabel = new JLabel("图书名称：");
		
		textFieldBookName = new JTextField();
		textFieldBookName.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("图书类别：");
		
		JLabel lblNewLabel_3 = new JLabel("作者：");
		
		textFieldAuthor = new JTextField();
		textFieldAuthor.setColumns(10);

		JLabel lblNewLabel_4 = new JLabel("出版社：");
		
		textFieldPublisher = new JTextField();
		textFieldPublisher.setColumns(10);
		

		comboBox.setEditable(true);
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"哲学", "社会科学", "政治", "军事", "经济", "文化", "文学", "艺术", "历史地理", "自然科学", "数理科学", "天文", "生物科学", "医学卫生", "农业科学", "工业技术", "交通运输", "航空航天", "环境科学", "综合性图书", "其他"}));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(68)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(lblNewLabel)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(textFieldBookName, GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE))
						.addGroup(gl_panel.createSequentialGroup()
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel_2)
								.addComponent(lblNewLabel_3)
								.addComponent(lblNewLabel_4))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(textFieldAuthor, GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
								.addComponent(textFieldPublisher, GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
								.addComponent(comboBox, 0, 109, Short.MAX_VALUE))))
					.addGap(45))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(21)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(textFieldBookName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_2)
						.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_3)
						.addComponent(textFieldAuthor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_4)
						.addComponent(textFieldPublisher, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(39, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);
	}
	
	/**
	 * 图书入库事件处理
	 */
	private void bookAddActionPerformed(ActionEvent e) throws IOException {
		String bookName=this.textFieldBookName.getText();
		String bookAuthor=this.textFieldAuthor.getText();
		String bookPublisher=this.textFieldPublisher.getText();
		String bookType=comboBox.getSelectedItem().toString();
		
		if(bookName==""||bookAuthor==""||bookPublisher=="") {
			JOptionPane.showMessageDialog(null,"未输入完整信息！");
			return;
		}
		else{
			Socket socket = new Socket(IP(), 2000);//此处主机地址请自行修改
			//设置超时时间
			socket.setSoTimeout(3000);
			//构建IO
			OutputStream os = socket.getOutputStream();
			InputStream is = socket.getInputStream();
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			//传输请求业务
			bw.write("26" + '\n'+bookName+'\n'+bookAuthor+'\n'+bookPublisher+'\n'+bookType+'\n');
			bw.flush();
			//从服务端输入到本地
			String str= br.readLine();
			System.out.println(str);
			System.out.println("传输结束");
			//输出输入流关闭
			os.close();
			socket.close();
			if (str.equals("true")) {
				JOptionPane.showMessageDialog(this, "添加成功!");
				this.dispose();
			} else {
				JOptionPane.showMessageDialog(this, "添加失败!");
			}
		}
	}
	/**
	 * 重置事件处理
	 * @param evt
	 */
	private void resetValueActionPerformed(ActionEvent evt) {
		this.resetValue();
	}
/**
 * 清空内容（重置）
 */
	private void resetValue() {
		this.textFieldBookName.setText("");
		this.textFieldAuthor.setText("");
		this.textFieldPublisher.setText("");
	}
}
